package com.xiaobug.demo.alipay;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

/**
 * Created by ljb on 2018/5/16.
 */

public class ItemMoveCallbackImpl extends ItemTouchHelper.Callback {

    private ItemMoveHelperListener mListener;

    public ItemMoveCallbackImpl(ItemMoveHelperListener itemMoveHelperListener) {
        mListener = itemMoveHelperListener;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int swipFlags = 0;
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        //只响应拖拽,没有滑动
        return makeMovementFlags(dragFlags, swipFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        //获取其实位置和目标位置
        int fromPosition = viewHolder.getAdapterPosition();
        int toPosition = target.getAdapterPosition();
        recyclerView.getAdapter().notifyItemMoved(fromPosition, toPosition);
        if (mListener != null)
            mListener.onItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    }

    @Override
    public boolean canDropOver(RecyclerView recyclerView, RecyclerView.ViewHolder current, RecyclerView.ViewHolder target) {
        return true;
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return false;
    }
}
